package com.hs.controller;

import com.hs.config.R;
import com.hs.dao.StudentMapper;
import com.hs.dao.UserMapper;
import com.hs.dao.YxClassMapper;
import com.hs.entity.User;
import com.hs.entity.YxClass;
import com.hs.service.UserService;
import com.hs.tool.StringTools;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    YxClassMapper yxClassMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    @RequestMapping("selectAll")
    @ResponseBody
    public List<User> selectAll(User map){
        userService.selectByCondition(map);
        return userService.selectByCondition(map);
    }
    @RequestMapping("selectAll0")
    @ResponseBody
    public List<User> selectAl0l(User map){
        return userMapper.selectByConditionB(map);
    }
    @RequestMapping("selectAll1")
    @ResponseBody
    public List<User> selectAll1(User map){
        return studentMapper.selectByCondition1(map);
    }
    @RequestMapping("selectAll2")
    @ResponseBody
    public List<YxClass> selectAll2(YxClass map){
        return yxClassMapper.selectByCondition(map);
    }
    @RequestMapping("insert")
    @ResponseBody
    public R insert(User user, Model  model) {
        if(user == null){
//            model.addAttribute("msg","请填写用户名密码等关键信息");
            return R.FAILURE("请填写用户名密码等关键信息",null);
        }
//        if(user.getFund() == null){
////            model.addAttribute("msg","请填写用户名密码等关键信息");
//            return R.FAILURE("请填写资产金额",null);
//        }
        if(StringTools.isEmpty(user.getName())){
            model.addAttribute("msg","请填写用户名");
            return R.FAILURE("请填写用户名",null);
        }
        if(StringTools.isEmpty(user.getPassWord())){
            model.addAttribute("msg","请填写密码");
            return R.FAILURE("请填写密码",null);
        }
        if(StringTools.isEmpty(user.getPhone())){
            model.addAttribute("msg","请填写手机号");
            return R.FAILURE("请填写手机号",null);
        }

        User p = new User();
        p.setName(user.getName());
        List<User> list = userService.selectByCondition(p);
        if(list.size() != 0){
            return R.FAILURE("用户已存在",null);
        }
        int i = userService.insertSelective(user);
        if(i > 0){
            return R.SUCCESS("",i);
        }
        return R.FAILURE("",0);
    }
    @RequestMapping("edit")
    @ResponseBody
    public R edit(@RequestParam Integer id, Integer status) {
        if(id == null){
//            model.addAttribute("msg","请填写用户名密码等关键信息");
            return R.FAILURE("用户id不能为空",null);
        }
        User user = new User();
        user.setId(id);
        user.setUpdateTime(new Date());
        user.setStatus(status);
        int i = userService.updateByPrimaryKeySelective(user);
        if(i > 0){
            return R.SUCCESS("",i);
        }
        return R.FAILURE("用户更新失败",0);
    }
    @RequestMapping("update")
    @ResponseBody
    public R update(User user) {
        if(user.getId() == null){
//            model.addAttribute("msg","请填写用户名密码等关键信息");
            return R.FAILURE("用户id不能为空",null);
        }
        user.setUpdateTime(new Date());
        int i = userService.updateByPrimaryKeySelective(user);
        if(i > 0){
            return R.SUCCESS("",i);
        }
        return R.FAILURE("用户更新失败",0);
    }

    @RequestMapping("a")
    @ResponseBody
    public R a() {
        System.out.println("一级缓存范围: " + sqlSessionFactory.getConfiguration().getLocalCacheScope());
        System.out.println("二级缓存是否被启用: " + sqlSessionFactory.getConfiguration().isCacheEnabled());
        return R.FAILURE("用户更新失败",0);
    }
    @RequestMapping("b")
    @ResponseBody
    public R b() {
        //根据 sqlSessionFactory 产生 session
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        //第一次查询，发出sql语句，并将查询出来的结果放进缓存中
        User u1 = userMapper.selectByPrimaryKey(2);
        System.out.println(u1);
        //第二次查询，由于是同一个sqlSession,会在缓存中查询结果
        //如果有，则直接从缓存中取出来，不和数据库进行交互
        User u2 = userMapper.selectByPrimaryKey(2);
        System.out.println(u2);
//        sqlSession.close();
        return R.FAILURE("用户更新失败",0);
    }









}
